<template>
  <div>
    <div style="margin: 10px 0">
      <el-input style="width: 200px" placeholder="请输入用户名" suffix-icon="el-icon-search" v-model="username"></el-input>
      <el-button class="ml-5" type="primary" @click="selectByTitle">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>

    <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg'"  @selection-change="handleSelectionChange">
      <el-table-column prop="id" label="ID" width="80"></el-table-column>
      <el-table-column prop="username" label="用户名" width="140"></el-table-column>
      <el-table-column prop="loginTime" label="登陆时间"></el-table-column>
      <el-table-column prop="state" label="登陆状态" width="170"></el-table-column>
      <el-table-column prop="ip" label="IP地址"></el-table-column>
      <el-table-column label="操作"  width="200" align="center">
        <template slot-scope="scope">
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="您确定删除吗？"
              @confirm="del(scope.row.id)"
          >
            <el-button type="danger" icon="el-icon-delete" circle slot="reference"></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

  </div>
</template>
<script>
export default {
  data() {
    return {
      username:'',
      tableData:[]
    }
  },

  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    //初始化页面
    selectAll(){
      let url="http://localhost:8080/usersLog";
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response)=>{
        let arr=response.data.data;
        for (let i = 0; i <arr.length; i++) {
          if (arr[i].state==0){
            arr[i].state="普通登陆失败"
          }else if (arr[i].state==1){
            arr[i].state="普通登陆成功"
          }else if (arr[i].state==2){
            arr[i].state="邮箱登陆失败"
          }else if (arr[i].state==3){
            arr[i].state="邮箱登陆成功"
          }else if (arr[i].state==4){
            arr[i].state="手机登陆失败"
          }else {
            arr[i].state="手机登陆成功"
          }
        }
        this.tableData=arr;
      })
    },
    //模糊查询
    selectByTitle(){
      let url="http://localhost:8080/usersLog/selectByUserName/"+this.username;
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response)=>{
        let arr=response.data.data;
        for (let i = 0; i <arr.length; i++) {
          if (arr[i].state==0){
            arr[i].state="普通登陆失败"
          }else if (arr[i].state==1){
            arr[i].state="普通登陆成功"
          }else if (arr[i].state==2){
            arr[i].state="邮箱登陆失败"
          }else if (arr[i].state==3){
            arr[i].state="邮箱登陆成功"
          }else if (arr[i].state==4){
            arr[i].state="手机登陆失败"
          }else {
            arr[i].state="手机登陆成功"
          }
        }
        this.tableData=arr;
      })
    },
    //模糊查询重置
    reset(){
      this.selectAll();
      this.username='';
    },
    //删除日志
    del(id){
    let url='http://localhost:8080/usersLog/delete/'+id;
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response)=>{
        if (response.data.state==20000){
          this.$message.success("删除成功")
          this.selectAll();
        }else {
          this.$message.error(response.data.message)
        }

      })
    }

  },
  mounted() {
  this.selectAll();
  }
}
</script>